---@class TBS.Bullet:TBS.EffectBase
local Bullet = TBS.CreateClass("Bullet","EffectBase")
function Bullet:OnCreate()
    TBS.EffectBase.OnCreate(self)
    self.speed = 15
    self.moveTime = 0
    self.maxMoveTime = 0
    self.startPos = Vector3.GetCacheVec()
    self.targetPos = Vector3.GetCacheVec()
    self.skill = nil
    self.isFixPos = false
    self.pathType = TBS.Config.BulletPathType.linear
    self.ctrlPoint = Vector3.GetCacheVec()
end
function Bullet:OnDestroy()
    TBS.EffectBase.OnDestroy(self)
    self.target = nil
    Vector3.ReturnToPool(self.startPos)
    Vector3.ReturnToPool(self.targetPos)
    Vector3.ReturnToPool(self.ctrlPoint)
    self.startPos = nil
    self.targetPos = nil
end
function Bullet:Play()
    TBS.EffectBase.Play(self)
    self.skill = self:GetSkill()
    self:InitMove()
end
function Bullet:DelayDie(time)
    self.isDestroying = true
    self.delayDieTime = time
    self.dieTime = 0
end
function Bullet:InitMove()
    self.startPos:SetV(self.pos)
    if self.isFixPos then
        return
    end
    local target = self:GetTarget()
    if target then
        local x,y = self:GetHitPos()
        self.targetPos:Set(x,y)
    else
        self.targetPos:Set(self.pos.x+10,self.pos.y,self.pos.z)
    end
end
function Bullet:SetFixTargetPos(x,y)
    self.isFixPos = true
    self.targetPos:Set(x,y)
end
function Bullet:SetPathType(pathType)
    self.pathType = pathType
end
function Bullet:SetCtrlPoint(x,y)
    self.ctrlPoint:Set(x,y)
end
function Bullet:GetHitPos()
    local target = self:GetTarget()
    if target then
        local pos = target:GetPos()
        return pos.x,pos.y
    end
    return 0,0
end
function Bullet:Update(dt)
    if self.isDie then
        return
    end
    if self.isDestroying then
        self.dieTime = self.dieTime + dt
        if self.dieTime >= self.delayDieTime then
            self:Die()
        end
        return
    end
    if self.during <= 0 then
        return
    end
    local t = self.curTime /self.during
    if t >= 1 then
        t = 1
    end
    if self.pathType == TBS.Config.BulletPathType.curve then
        local x = (1 - t) * (1 - t) * self.startPos.x + 2 * t * (1 - t) * self.ctrlPoint.x + t * t * self.targetPos.x
        local y = (1 - t) * (1 - t) * self.startPos.y + 2 * t * (1 - t) * self.ctrlPoint.y + t * t * self.targetPos.y
        self.pos:Set(x,y)
    else
        Vector3.Lerp2(self.startPos,self.targetPos,t,self.pos)
    end
    self.curTime = self.curTime + dt
    self:SetObjPos(self.pos)
    if t >= 1 then
        self:Hit()
        self:DelayDie(self.delayDieTime)
    end
end
function Bullet:Hit()
    --self:GetSkill():Hit(self:GetTarget())
    local skill = self:GetSkill()
    local target = self:GetTarget()
    if skill then
        skill:Hit( target,self.targetPos.x,self.targetPos.y)
    end
end